package com.hmall.order.web;


import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.common.feign.AddressClient;
import com.hmall.common.feign.SearchClient;

import com.hmall.order.config.SnowFlake;
import com.hmall.order.constants.ItemMqConstants;
import com.hmall.order.interceptor.BaseContext;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.OrderParam;
import com.hmall.order.service.IOrderService;

import com.hmall.order.service.IOrderdetailService;
import com.hmall.order.service.OrderLogisticsService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("order")
public class OrderController {

   @Autowired
   private RabbitTemplate rabbitTemplate;

   @Autowired
   private IOrderService orderService;
   @Autowired
   private IOrderdetailService iOrderdetailService;
   @Autowired
   private OrderLogisticsService orderLogisticsService;

   @Autowired
   private AddressClient addressClient;

   @Autowired
   private SearchClient searchClient;

   @GetMapping("{id}")
   public Order queryOrderById(@PathVariable("id") Long orderId) {
      return orderService.getById(orderId);
   }

   @PostMapping
   public Long saveOrder(@RequestBody OrderParam orderParam) throws InterruptedException {
//      String uuid = UUID.randomUUID().toString();
//      long l = Long.parseLong(uuid);

      System.out.println("==============================================================");
      System.out.println(BaseContext.getCurrentId());
      System.out.println("==============================================================");

      long snowFlake = SnowFlake.nextId();
      Item item = searchClient.getId(orderParam.getItemId());
      long total = item.getPrice() * orderParam.getNum();

      Order order = new Order();
      order.setId(snowFlake);
      order.setTotalFee(total);
      order.setStatus(1);
      order.setPaymentType(orderParam.getPaymentType());

      order.setUserId(BaseContext.getCurrentId());
      Date date = new Date(System.currentTimeMillis());
      order.setCreateTime(date);
      orderService.save(order);

//      rabbitTemplate.convertAndSend(ItemMqConstants.EXCHANGE_NAME, ItemMqConstants.INSERT_KEY,
//              order.getId());

      rabbitTemplate.convertAndSend("delay.direct","delay",order.getId(),
              msg -> {
                 msg.getMessageProperties()
                         .setHeader("x-delay", 10000); // 10000=10s   1800000
                 return msg;
              });

      OrderDetail orderDetail = new OrderDetail();
      orderDetail.setOrderId(order.getId());
      orderDetail.setItemId(item.getId());
      orderDetail.setNum(orderParam.getNum());
      orderDetail.setName(item.getName());

      orderDetail.setSpec(item.getSpec());
      orderDetail.setPrice(item.getPrice());
      orderDetail.setImage(item.getImage());
      orderDetail.setCreateTime(date);
      orderDetail.setUpdateTime(date);
      iOrderdetailService.save(orderDetail);



      Address address = addressClient.findAddressById(orderParam.getAddressId());
      OrderLogistics orderLogistics = new OrderLogistics();
      orderLogistics.setOrderId(snowFlake);
      orderLogistics.setContact(address.getContact());
      orderLogistics.setMobile(address.getMobile());
      orderLogistics.setProvince(address.getProvince());
      orderLogistics.setCity(address.getCity());
      orderLogistics.setTown(address.getTown());
      orderLogistics.setStreet(address.getStreet());
      orderLogistics.setCreateTime(date);
      orderLogistics.setUpdateTime(date);
      orderLogisticsService.save(orderLogistics);


      searchClient.updateStock(orderParam.getItemId(),orderParam.getNum());
//      TimeUnit.SECONDS.sleep(10);
//      searchClient.updateStockRecover(23032627200L,orderParam.getNum());



      return order.getId();
   }







}
